﻿using UnityEngine;
using System.Collections;

public class PasswordButton : MonoBehaviour {
    int ButtonIndex ;           //按钮的序号
    protected float PressDownTime = 1f;   //凝视按钮触发时间
    protected float PressDeep = 0.15f;
    protected float TimeCounter = 0;      //时间计时器
    protected bool isEnter = false;       //射线检测是否Enter Button 
    public GameObject Roller;   //密码箱的转轴
    void Start()
    {
        ButtonIndex = System.Convert.ToInt32(this.name);
    }
    /// <summary>
    /// 检测射线进入后的操作
    /// </summary>
	public void TriggerEnter()
    {
        TimeCounter = 0; //时间计时器清零
        isEnter = true;
    }
    /// <summary>
    /// 检测射线移出后的操作
    /// </summary>
    public void TriggerOut()
    {
        isEnter = false;
    }
    /// <summary>
    /// 按钮被按下操作
    /// </summary>
    void PressDown()
    {
        transform.localPosition += new Vector3(0, 0, PressDeep); //按钮被按下
        Roller.GetComponent<PasswordRoate>().GetBtnMessage(ButtonIndex); //密码箱转轴接收按钮按下信息
        GetComponent<PasswordButton>().enabled = false; //此按钮控制脚本禁用
    }
    /// <summary>
    /// 输入错误，按钮恢复
    /// </summary>
    public void Checkout()
    {
        transform.localPosition -= new Vector3(0, 0, PressDeep); //按钮抬起
    }

    void Update()
    {
        if (isEnter)
        {
            TimeCounter += Time.deltaTime; //检测射线进入即开始计时
        }
        if (TimeCounter > PressDownTime)
        {
            TimeCounter = 0f;//计时器清零
            isEnter = false;
            PressDown();     //达到时间执行按钮被按下操作
        }
    }
}
